package com.shoppingmall.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.allen.library.RxHttpUtils;
import com.allen.library.interceptor.Transformer;
import com.allen.library.observer.CommonObserver;
import com.example.zyh.toolslib.base.BaseActivity;
import com.example.zyh.toolslib.utils.EmptyUtil;
import com.example.zyh.toolslib.utils.SPUtils;
import com.example.zyh.toolslib.utils.ToastUtil;
import com.shoppingmall.R;
import com.shoppingmall.bean.LoginBean;
import com.shoppingmall.bean.RegisterBean;
import com.shoppingmall.data.DataApi;
import com.shoppingmall.retrofitUtils.RetrofitHelp;
import com.shoppingmall.widget.TimeTextLogin;

import java.io.IOException;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.image_login)
    ImageView imageLogin;
    @BindView(R.id.username)
    EditText username;
    @BindView(R.id.re_username)
    RelativeLayout reUsername;
    @BindView(R.id.btn_get_code)
    Button btnGetCode;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.re_password)
    RelativeLayout rePassword;
    @BindView(R.id.re_bnt_login)
    RelativeLayout reBntLogin;
    @BindView(R.id.goto_register)
    TextView gotoRegister;
    @BindView(R.id.image_wx)
    ImageView imageWx;
    @BindView(R.id.wx_login)
    RelativeLayout wxLogin;
    @BindView(R.id.imageback)
    RelativeLayout imageback;

    private TimeTextLogin mTimeText;

    @Override
    protected int initLayout() {
        return R.layout.login_layout;
    }

    @Override
    protected void initView() {
        if(!EmptyUtil.isEmpty(username.getText().toString())){
            password.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if(!EmptyUtil.isEmpty(s.length()>0)){
                        reBntLogin.setBackgroundResource(R.drawable.ic_login_true_bnt);
                    }else{
                        reBntLogin.setBackgroundResource(R.drawable.ic_login_no_bnt);
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {
                    if(!EmptyUtil.isEmpty(s.length()>0)){
                        reBntLogin.setBackgroundResource(R.drawable.ic_login_true_bnt);
                    }else{
                        reBntLogin.setBackgroundResource(R.drawable.ic_login_no_bnt);
                    }

                }
            });
        }


    }

    @Override
    protected void initData() {
//        StatusBarUtil.setTranslucentForImageViewInFragment(this, 0,null);
    }

    @Override
    protected void initEvent() {

    }



    @OnClick({R.id.btn_get_code, R.id.password, R.id.re_bnt_login, R.id.goto_register, R.id.wx_login, R.id.imageback})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_get_code:

                if(EmptyUtil.isEmpty(username.getText().toString())){
                    ToastUtil.showS(LoginActivity.this, "请输入手机号码");
                    return;
                }else{
                    if(isPhoneNumber(username.getText().toString())==false){
                        ToastUtil.showS(LoginActivity.this, "请输入正确的手机号码");
                        return;
                    }
                }

                getLoginsms(username.getText().toString());

                break;
            case R.id.password:
                break;
            case R.id.re_bnt_login:
                if(EmptyUtil.isEmpty(username.getText().toString())){
                    ToastUtil.showS(LoginActivity.this, "请输入手机号码");
                    return;
                }else{
                    if(isPhoneNumber(username.getText().toString())==false){
                        ToastUtil.showS(LoginActivity.this, "请输入正确的手机号码");
                        return;
                    }
                }

                if(EmptyUtil.isEmpty(password.getText().toString())){
                    ToastUtil.showS(LoginActivity.this, "请输入验证码");
                    return;
                }
                Login(username.getText().toString(),password.getText().toString());

                break;
            case R.id.goto_register:
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.wx_login:

                break;
            case R.id.imageback:
                this.finish();
                break;
        }
    }


    /**
     * 获取验证码
     */
    public  void getLoginsms(String phone){
        RxHttpUtils.createApi(DataApi.class)
                .Loginsmscode(phone,"786s","uuid")
                .compose(Transformer.<ResponseBody>switchSchedulers())
                .subscribe(new CommonObserver<ResponseBody>() {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(ResponseBody smsCodeBean) {
                        mTimeText = new TimeTextLogin(60000, 1000, btnGetCode);
                        mTimeText.start();
                    }
                });
    }


    /**
     * 登录
     */

    public void Login(String phone,String smscode){
        RxHttpUtils.createApi(DataApi.class)
                .Login(phone, smscode)
                .compose(Transformer.<LoginBean>switchSchedulers())
                .subscribe(new CommonObserver<LoginBean>() {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(LoginBean loginBean) {
                        SPUtils.put(LoginActivity.this,"token",loginBean.getData().getAccess_token());
                    }
                });

    }


}
